/** ***********************************************************************************
* @file         rsa_driver.h
* @brief        rsa1024操作部分
* @details      
* @author       
* @date         2021/3
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/

#ifndef _BT_RSA_DRIVER_H_
#define _BT_RSA_DRIVER_H_



/* error codes [will be expanded in future releases] */
enum {
	CRYPT_OK = 0,             /* Result OK */
	CRYPT_ERROR,            /* Generic Error */
	CRYPT_NOP,              /* Not a failure but no operation was performed */

	CRYPT_INVALID_KEYSIZE,  /* Invalid key size given */
	CRYPT_INVALID_ROUNDS,   /* Invalid number of rounds */
	CRYPT_FAIL_TESTVECTOR,  /* Algorithm failed test vectors */

	CRYPT_BUFFER_OVERFLOW,  /* Not enough space for output */
	CRYPT_INVALID_PACKET,   /* Invalid input packet given */

	CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
	CRYPT_ERROR_READPRNG,   /* Could not read enough from PRNG */

	CRYPT_INVALID_CIPHER,   /* Invalid cipher specified */
	CRYPT_INVALID_HASH,     /* Invalid hash specified */
	CRYPT_INVALID_PRNG,     /* Invalid PRNG specified */

	CRYPT_MEM,              /* Out of memory */

	CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
	CRYPT_PK_NOT_PRIVATE,   /* Requires a private PK key */

	CRYPT_INVALID_ARG,      /* Generic invalid argument */
	CRYPT_FILE_NOTFOUND,    /* File Not Found */

	CRYPT_PK_INVALID_TYPE,  /* Invalid type of PK key */
	CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */
	CRYPT_PK_DUP,           /* Duplicate key already in key ring */
	CRYPT_PK_NOT_FOUND,     /* Key not found in keyring */
	CRYPT_PK_INVALID_SIZE,  /* Invalid size input for PK parameters */

	CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
	CRYPT_PK_INVALID_PADDING /* Invalid padding on input */
};

enum ltc_pkcs_1_v1_5_blocks
{
	LTC_LTC_PKCS_1_EMSA = 1,        /* Block type 1 (LTC_PKCS #1 v1.5 signature padding) */
	LTC_LTC_PKCS_1_EME = 2         /* Block type 2 (LTC_PKCS #1 v1.5 encryption padding) */
};



//	unsigned char base64Encode(unsigned char * inStr, u16 inLen,
//		unsigned char * outStr, u16 * outLen);
unsigned char base64Encode(unsigned char * inStr, unsigned int inLen,
	unsigned char * outStr, unsigned int * outLen);
unsigned char base64Decode(unsigned char * inStr, UINT16 inLen,
	unsigned char * outStr, UINT16 * outLen);

void GetRSAKeyFromPem(UINT8 * pDa, UINT16 * len);
void testRSA(void);
int RSA1024bitCalc(unsigned char *n, unsigned char *e, unsigned char *c, unsigned char *t);


int pkcs_1_v1_5_decode(const unsigned char *msg,
	unsigned int  msglen,
	unsigned int  block_type,
	unsigned int  modulus_bitlen,
	unsigned char *out,
	unsigned int *outlen,
	unsigned int *is_valid);

int prng_is_valid(int idx);

int pkcs_1_v1_5_encode(const unsigned char *msg,
	unsigned int  msglen,
	unsigned  block_type,
	unsigned int  modulus_bitlen,
	unsigned char prng,
	unsigned  prng_idx,
	unsigned char *out,
	unsigned int *outlen);


UINT32 BLEanalyRSA(UINT8 *pCipheredData, UINT16 len, UINT8 *pPubKey, UINT16 pubKeyLen, UINT8 * outDa);

#endif
